package com.hong;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test005 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        //1.使用java的反身机制获取的类的所有属性 方法 并且为私有属性赋值
        Class<?> forName = Class.forName("com.hong.User");
        //2.获取到当前类的所有属性
        /*Field[] declaredFields = forName.getDeclaredFields();
        for (Field field:declaredFields){
            System.out.println(field.getName());
        }
        //3.获取当前类的所有的方法
        Method[] declaredMethods = forName.getDeclaredMethods();
        for (Method method:declaredMethods){
            System.out.println(method.getName());
        }*/
        //4.使用java的反射机制给私有属性赋值
        Object newInstance = forName.newInstance();
        Field id = forName.getDeclaredField("id");
        id.setAccessible(true);
        id.set(newInstance,"20");
        Field name = forName.getDeclaredField("name");
        name.setAccessible(true);
        name.set(newInstance,"测试");
        User user = (User)newInstance;
        System.out.println(user);
    }
}
